<?php

namespace App\Http\Controllers\Admin;

use App\Http\Models\News;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class NewsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $list = News::orderBy("created_at","Desc")->orderBy("id","Desc")->paginate(10);
        return view("admin.news.index",compact('list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("admin.news.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,News $news)
    {
        $this->checkData($request);

        $news->title   = $request->title;
        $news->keyword = $request->keyword;
        $news->desc    = $request->desc;
        $news->remark  = $request->remark;
        $news->views   = $request->views;
        $news->content = $request->contents;

        checkReturn($news->save(),"保存成功");
        return redirect(route('admin.news.create'));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Http\Models\News  $news
     * @return \Illuminate\Http\Response
     */
    public function show(News $news)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Http\Models\News  $news
     * @return \Illuminate\Http\Response
     */
    public function edit(News $news)
    {
        return view("admin.news.edit",compact('news'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Http\Models\News  $news
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, News $news)
    {
        $this->checkData($request);

        $data = [
            'title'   => $request->title,
            'keyword' => $request->keyword,
            'desc'    => $request->desc,
            'remark'  => $request->remark,
            'views'   => $request->views,
            'pic'     => $request->pic,
            'content' => $request->contents,
        ];

        if($news->pic != $request->pic){
            dump($news->pic);
            dump(Storage::delete($news->pic));
        }

        $result = $news::where(['id'=>$news->id])->update($data);
        checkReturn($result,"编辑成功");
        return redirect(route('admin.news.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Http\Models\News  $news
     * @return \Illuminate\Http\Response
     */
    public function destroy(News $news)
    {
        $result = (new News)::destroy($news->id);
        //删除旧图片
        Storage::delete($news->pic);
        checkReturn($result,"删除");
        return redirect(route('admin.news.index'));
    }

    /**
     * 请求校验
     */
    public function checkData($request){
        $request->validate([
            'title'    => 'required|min:2|max:16',
            'views'    => 'required|integer',
            'contents' => 'required',
        ],[
            'title.required' => '新闻标题不能为空',
            'title.min'      => '新闻标题最少2个字',
            'title.max'      => '新闻标题最多16个字',
            'views.required' => '浏览次数不能为空',
            'views.integer'  => '浏览次数必须为数值',
            'contents.required' => '新闻内容不可为空',
        ]);
    }
}
